<?php

require_once "database/DatabaseController.php";
require_once('includes/smarty/ACBSmarty.php');
require_once('includes/authentication/Authenticator.php');

/**
 * Default implementation of the ACBController.
 * 
 * Handles calls from the originating page and delegates them to the proper objects.
 *
 * @author A.J. Janssen
 */
class ACBController {
    
    /** @var ACBSmarty */
    protected $acb;
    
    /** @var Authenticator */
    public $auth;
    
    /** @var DataBaseController */
    protected $dbc;
    
    function __construct()
    {
        $this->acb = new ACBSmarty();
        $this->dbc = new DataBaseController();
        $this->auth = new Authenticator($this->dbc);
    }
    
    function isAuthenticated()
    {
        return $this->auth->isAuthenticated();
    }
    
    function assign($var1, $var2)
    {
        $this->acb->assign($var1, $var2);
    }
    
    function display($template)
    {
        $this->updateAuthenticationStatus();
        $this->acb->display($template);
    }
    
    private function updateAuthenticationStatus()
    {
        $authentication = ($this->auth->isAuthenticated()) ? 1 : 0;
        $this->acb->assign('authenticated', $authentication);
        if ($this->auth->isAuthenticated())
        {
            $this->acb->assign('username', $this->auth->getUser()->username);
            $this->acb->assign('currentUser', $this->auth->getUser());
        }
    }
    
    public function logout()
    {
        $this->auth->logout();
    }
}

?>
